package us.lithub.ui.activity;

import java.util.List;

import us.lithub.R;
import us.lithub.Util;
import us.lithub.data.Friend;
import us.lithub.data.WebConnector;
import us.lithub.ui.RowFriend;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * The Class Friends displays a list of friends.
 */
public class Friends extends Activity {

	private static final int REQUEST_ADD_FRIEND = 1;
	private WebConnector wc;
	private Intent mIntent;
	private String username;
	private String password;

	/** The Constant PICK_FRIEND_ON_CLICK. (pass this as the intent mode if you want to pick a friend on click rather than browse their library) */
	public static final int PICK_FRIEND_ON_CLICK = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friends);
		setResult(RESULT_CANCELED);
		
		username = Util.getUsername(this);
		password = Util.getPassword(this);
		
		wc = WebConnector.getInstance();
		mIntent = getIntent();
		loadData(mIntent.getIntExtra(Util.MODE, 0));
	}

	private void loadData(final int mode) {
		new Thread() {
			
			@Override
			public void run() {
				final List<Friend> friends = wc.getAllFriends(username, password);
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// Create the adapter and bind data to the list view
						FriendAdapter adapter = new FriendAdapter(Friends.this, R.layout.row_friend, friends);
						ListView lv = (ListView) findViewById(R.id.lvListView);
						lv.setAdapter(adapter);
						lv.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {							
								if (mode == Util.MODE_PICK_FRIEND) {
									mIntent.putExtra(Friend.NAME, friends.get(position).getUsername());
									setResult(Activity.RESULT_OK, mIntent);
									finish();
								} else {
									Intent intent = new Intent(Friends.this, TheirLibrary.class);
									intent.putExtra(Friend.NAME, friends.get(position).getUsername());
									startActivity(intent);
									finish();
								}
							}
						});	
					}
				});
			};
		}.start();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == REQUEST_ADD_FRIEND) {
			if (resultCode == RESULT_OK) {

			} else if (resultCode == RESULT_CANCELED) {

			}
		}
	}

	private class FriendAdapter extends ArrayAdapter<Friend> {
		
		private Context context;		
		private List<Friend> friends;

		public FriendAdapter(Context context, int textViewResourceId, List<Friend> friends) {
			super(context, textViewResourceId, friends);
			this.context = context;
			this.friends = friends;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;

			if (v == null) {
				LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row_friend, null);
			}

			Friend friend = friends.get(position);

			if (friends != null) {
				((RowFriend) v).setName(friend.getUsername());
				((RowFriend) v).setImage();
			}
			return v;
		}	
	}
}
